package com.hspedu.inputstream;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/22
 * @time 18:35
 */
public class FileInputStream_ {
    public static void main(String[] args) {

    }

    @Test
    public void readFile01() {
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\news5.txt");
            //返回-1表示读取完毕
            while ((readData = fileInputStream.read()) != -1) {
                char c = (char) readData;
                System.out.print(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流，释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void readFile02() {
        int readData = 0;
        FileInputStream fileInputStream = null;
        //字节缓冲数组
        byte[] buffer = new byte[8];

        //字节输入流每次读取的实际长度
        int readLength = 0;
        try {
            fileInputStream = new FileInputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\news1.txt");
            //返回-1表示读取完毕
            while ((readLength = fileInputStream.read(buffer)) != -1) {
                String s = new String(buffer,0,readLength);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流，释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
